import { QuickActionMode } from './common';
import { QuickActionModeOption } from './interface';

/**
 * Quick Action Mode Register
 *
 * @author chitanda
 * @date 2022-06-28 14:06:15
 * @export
 * @class QuickActionModeRegister
 */
export class QuickActionModeRegister {
  protected modes: QuickActionMode[] = [];

  /**
   * 当前注册的模式数量
   *
   * @author chitanda
   * @date 2022-06-30 10:06:26
   * @readonly
   * @type {number}
   */
  get size(): number {
    return this.modes.length;
  }

  /**
   * 增加新的模式
   *
   * @author chitanda
   * @date 2022-06-28 14:06:18
   * @param {QuickActionModeOption} mode
   */
  addModel(opts: QuickActionModeOption): void {
    if (this.modes.find(m => m.opts.type === opts.type)) {
      throw new Error(`QuickActionMode.type ${opts.type} already exists`);
    }
    const index = this.modes.findIndex(m => opts.keyboards && m.opts.keyboards && opts.keyboards === m.opts.keyboards);
    if (index !== -1) {
      throw new Error(`QuickActionMode.keyboards ${opts.keyboards} already exists`);
    }
    this.modes.push(new QuickActionMode(opts));
  }

  /**
   * 删除模式
   *
   * @author chitanda
   * @date 2022-06-28 19:06:24
   * @param {string} type 模式类型
   */
  removeModel(type: string): void {
    const index = this.modes.findIndex(m => m.opts.type === type);
    if (index === -1) {
      throw new Error(`QuickActionMode ${type} not exists`);
    }
    const mode = this.modes[index];
    this.modes.splice(index, 1);
    mode.destroyed();
  }

  getModes(): QuickActionMode[] {
    return this.modes;
  }
}
// 模式注册中心
export const quickActionModeRegister = new QuickActionModeRegister();
